#include <iostream>
#include <queue>
#include <vector>
#include <unordered_map>
using namespace std;

int extend(queue<string>& q,unordered_map<string,int>& hasa,unordered_map<string,int>& hasb,vector<string>& h1,vector<string>& h2)
{
	int d=hasa[q.front()];
	while(q.size() && hasa[q.front()]==d)
	{
		string s=q.front();
		q.pop();
		for(int i=0;i<h1.size();i++)
		{
			for(int j=0;j<s.size();j++)
			{
				if(s.substr(j,h1[i].size())==h1[i])
				{
					string tmp=s.substr(0,j)+h2[i]+s.substr(j+h1[i].size());
					if(hasb.count(tmp))
					{
					    return hasa[s]+hasb[tmp]+1;	
					}
					if(hasa.count(tmp))
					{
					    continue;
					}
						

					hasa[tmp]=hasa[s]+1;
					q.push(tmp);	
				}
			}
		}	
	}
	return 11;
}
int main()
{
	string a,b;
	cin>>a>>b;
	string s1,s2;
	vector<string> h1,h2;
	while(cin>>s1>>s2)
	{
		h1.push_back(s1);
		h2.push_back(s2);
	}
	if(a==b)
	{
		cout<<0;
		return 0;
	}
	unordered_map<string,int> hasa,hasb;
	hasa[a]=hasb[b]=0;
	
	queue<string> qa,qb;
	qa.push(a);
	qb.push(b);
	int step=0;
	int ret;
	while(qa.size() && qb.size())
	{
		step++;
		ret=11;
		if(qa.size()<qb.size())
			ret=extend(qa,hasa,hasb,h1,h2);
		else
			ret=extend(qb,hasb,hasa,h2,h1);
		if(ret<=10)
			break;
		if(step==10)
			break;
	}
	if(ret<=10 && step<=10)
	{
		cout<<ret<<endl;
	}
	else{
		cout<<"NO ANSWER!"<<endl;
	}
	return 0;
}
